package com.qinge.blog.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qinge.blog.entity.*;
import com.qinge.blog.utils.CurrentUserUtil;
import com.qinge.blog.vo.paging.PagingReqVO;
import org.springframework.stereotype.Service;
import com.qinge.blog.dao.BlogCommentDao;
import com.qinge.blog.service.BlogCommentService;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @author QDW
 * @date 2022-05-02 23:38:51
 * @description BlogComment service层实现类
 */
@Service
public class BlogCommentServiceImpl  implements BlogCommentService {
    @Resource
    BlogCommentDao commentDao;




    @Override
    public void add(BlogComment... blogComments) {
        for (BlogComment comment:blogComments) {
           comment.setUserId(CurrentUserUtil.getUserId());
        }

        commentDao.batchInsert(Arrays.asList(blogComments));
    }

    @Override
    public void addByList(List<BlogComment> blogComments) {
        for (BlogComment comment:blogComments) {
            comment.setUserId(CurrentUserUtil.getUserId());
        }

        commentDao.batchInsert(blogComments);
    }

    @Override
    public void removeById(String id) {
        commentDao.deleteByPrimaryKey(id);
    }

    @Override
    public void removeBatchByIds(List<String> ids) {
        BlogCommentExample example=new BlogCommentExample();
        BlogCommentExample.Criteria criteria=example.createCriteria();

        criteria.andIdIn(ids);

        commentDao.deleteByExample(example);
    }

    @Override
    public void update(BlogComment blogComment) {
        commentDao.upsert(blogComment);
    }

    @Override
    public void upsert(BlogComment blogComment) {
        commentDao.upsert(blogComment);
    }

    @Override
    public BlogComment findById(String id) {
        return commentDao.selectByPrimaryKey(id);
    }

    @Override
    public List<BlogComment> findByIds(List<String> ids) {
        BlogCommentExample example=new BlogCommentExample();
        BlogCommentExample.Criteria criteria=example.createCriteria();

        criteria.andIdIn(ids);

        return commentDao.selectByExample(example);
    }

    @Override
    public PageInfo<BlogComment> findAll() {
        return paging(new PagingReqVO<BlogComment>());
    }

    @Override
    public PageInfo<BlogComment> findAll(PagingReqVO<BlogComment> pagingReqVO) {
        return paging(pagingReqVO);
    }

    @Override
    public PageInfo<BlogComment> paging(PagingReqVO<BlogComment> pagingReqVO) {
        PageHelper.startPage(pagingReqVO);

        BlogCommentExample example=new BlogCommentExample();
        BlogCommentExample.Criteria criteria=example.createCriteria();

        if(!CurrentUserUtil.isAdmin()){
            criteria.andUserIdEqualTo(CurrentUserUtil.getUserId());
        }

        List<BlogComment> list=commentDao.selectByExample(example);

        return new PageInfo<BlogComment>(list);
    }
}

